// pages/userCenter/index.js
const app = getApp()
const ip=app.globalData.ip
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeid:0,
    activeNames: ['1'],
    code:'',
    iv:'',
    encryptedData:'',
    userInfo:{},
    buttonHidden:true,
    openid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let openid=wx.getStorageSync('openid');
    console.log(openid)
    if(openid!=''){
      this.setData({
        openid:openid,
        buttonHidden:false,
        userInfo:wx.getStorageSync('userInfo')
      })
    }
    this.getStoreid()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.setData({
    //   openid:wx.getStorageSync('openid')
    // })
    this.getStoreid()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      openid:wx.getStorageSync('openid')
    })
    this.getStoreid()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //初始没有小店，用户创建小店后从缓存获取得storeid方法
  getStoreid(){
    let storeid=wx.getStorageSync('storeid');
    if(storeid!=null){
      this.setData({
        storeid:storeid,
      })
    }else{//如果缓存没有用openid查询

    }
  },
  myStore(){
    if(this.data.storeid==0){
      wx.showModal({
        title: '',
        content: '请先开设小店',
        success: function (res) {
          if (res.confirm) {//这里是点击了确定以后
            console.log('用户点击确定')
          } else {//这里是点击了取消以后
            console.log('用户点击取消')
          }
        }
      })
    }else{
      wx.navigateTo({
        url: './myShop/index?storeid='+this.data.storeid,
      })
    }
  },
//暂未上线
alertCollection(){
  wx.navigateTo({
    url: './myCollect/index?userid='+wx.getStorageSync('userid'),
    
  })
},
onChange(event) {
  this.setData({
    activeNames: event.detail,
  });
},

getAuth(){
let code=''
wx.getUserProfile({
  desc: 'desc',
  success:e=>{
    console.log(e)
    this.setData({
      userInfo: e.userInfo,
      buttonHidden:false,
      // hasUserInfo: true,
      code:code,
      iv:e.iv,
      encryptedData:e.encryptedData
    });
    wx.login({
      success(res){
        code=res.code
        wx.request({
          url: ip+"/user/getWxUserOpenid",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
            },
          data:{
            iv:e.iv,
            encryptedData:e.encryptedData,
            code:code,
          },
          method:'POST',
          success:(res)=>{
            console.log(res)
              //缓存
              wx.setStorage({
                key:"userInfo",
                data:e.userInfo,
              });
              wx.setStorage({
                key:"openid",
                data:res.data.data.openid,
              });
              wx.setStorage({
                key:"userid",
                data:res.data.data.userid,
              });
              wx.setStorage({
                key:"token",
                data:res.data.data.token,
              });
              wx.setStorage({
                key:"storeid",
                data:res.data.data.storeid,
              });
                        //授权后跳转
              wx.switchTab({
                url: '../homePage/index',
              })
              
          }
        })
      }
      })
  }
})
}
})